package com.example.test622;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;

import java.util.Map;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.android.AndroidDisplayer;
import master.flame.danmaku.danmaku.model.android.BaseCacheStuffer;
//自定义view
public class MyBaseCahe extends BaseCacheStuffer {
    private float LEFTMARGE;
    private float RIGHTMARGE;
    private float TEXT_SIZE;
    private float IMAGE_HEIGHT;
    //构造方法初始化大小
    public MyBaseCahe(Activity activity) {

        LEFTMARGE=activity.getResources().getDimension(R.dimen.dp_21);
        RIGHTMARGE=activity.getResources().getDimension(R.dimen.dp_23);
        IMAGE_HEIGHT=activity.getResources().getDimension(R.dimen.dp_26);
        TEXT_SIZE=activity.getResources().getDimension(R.dimen.dp_19);

    }
    //测量
    @Override
    public void measure(BaseDanmaku danmaku, TextPaint paint, boolean fromWorkerThread) {
        Map<String,Object> map=(Map<String, Object>) danmaku.tag;
        String content=(String) map.get("content");
        Bitmap bitmap=(Bitmap) map.get("bitmap");
        paint.setTextSize(TEXT_SIZE);
        float contentwidth=paint.measureText(content);
        danmaku.paintWidth=contentwidth+LEFTMARGE+RIGHTMARGE+IMAGE_HEIGHT;
        danmaku.paintHeight=IMAGE_HEIGHT*1.2F;

    }
    @Override
    public void clearCaches() {

    }

    @Override
    public void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, float top,
                            boolean fromWorkerThread, AndroidDisplayer.DisplayerConfig displayerConfig) {
        //初始化数据
        Map<String,Object> map=(Map<String, Object>) danmaku.tag;
        String content=(String) map.get("content");
        Bitmap bitmap=(Bitmap) map.get("bitmap");
        String color=(String) map.get("color");
        //定义画笔
        Paint paint=new Paint();
        paint.setTextSize(TEXT_SIZE);
        //绘制背景
        int textlength=(int) paint.measureText(content);
        paint.setColor(Color.parseColor(color));
        //绘制边框
        float reacfbgletf=left;
        float reacfbgtop=top;
        float reacfbgright=left+textlength+IMAGE_HEIGHT+LEFTMARGE+RIGHTMARGE;
        float reacfbgBottom=top+IMAGE_HEIGHT;
        canvas.drawRoundRect(new RectF(reacfbgletf,reacfbgtop,reacfbgright,reacfbgBottom)
                ,IMAGE_HEIGHT/2,IMAGE_HEIGHT/2,paint);
        //绘制头像
        float avatorRight=left+IMAGE_HEIGHT;
        float avatorBottom=top+IMAGE_HEIGHT;
        canvas.drawBitmap(bitmap,null,new RectF(left,top,avatorRight,avatorBottom),paint);
        //绘制文本
        paint.setColor(Color.WHITE);
        float contentleft=left+IMAGE_HEIGHT+LEFTMARGE;
        //计算偏移量，通过画笔获取便宜量
        Paint.FontMetrics fontMetrics=paint.getFontMetrics();
        float textTop=fontMetrics.top;
        float textBottom=fontMetrics.bottom;
        float  contentBottom=top+IMAGE_HEIGHT/2;
        int baseLineY=(int) (contentBottom-textTop/2-textBottom/2);
        //文本
        canvas.drawText(content,contentleft,baseLineY,paint);
    }


}

